import { Injectable } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { ProfessionDTO } from 'src/app/project/model/icm/icm-base/profession/profession.dto';
import { ApiSimpleData } from 'src/app/project/model/common/api-simple-data';

@Injectable({
  providedIn: 'root',
})
export class ProfessionService {
  private static URL = '/api/icm-profession';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询专业信息
   * @param option 分页参数
   * @param code 专业编码
   * @param name 专业名称
   * @param academyId 所属学院id
   */
  findOnePage(
    option: QueryOptions,
    code?: string,
    name?: string,
    academyId?: string,
  ): Observable<ApiPagedData<ProfessionDTO>> {
    const params = {};
    Object.assign(params, option, code ? { code } : {}, name ? { name } : {}, academyId ? { academyId } : {});
    return this.http.get<ApiPagedData<ProfessionDTO>>(ProfessionService.URL, params);
  }

  /**
   * 添加专业
   * @param professionDTO 专业信息
   */
  create(professionDTO: ProfessionDTO): Observable<ProfessionDTO> {
    return this.http.post<ProfessionDTO>(ProfessionService.URL, professionDTO);
  }

  /**
   * 根据id查找专业
   * @param id 专业id
   * @param isGetClass 是否获取班级
   */
  findById(id: string, isGetClass: boolean): Observable<ProfessionDTO> {
    const params = {};
    Object.assign(params, isGetClass != null ? { isGetClass } : {});
    return this.http.get<ProfessionDTO>(`${ProfessionService.URL}/${id}`, params);
  }

  /**
   * 修改更新专业信息
   * @param id 专业id
   * @param professionDTO 专业信息
   */
  complete(id: string, professionDTO: ProfessionDTO): Observable<ProfessionDTO> {
    return this.http.put<ProfessionDTO>(`${ProfessionService.URL}/${id}`, professionDTO);
  }

  /**
   * 根据id删除专业
   * @param id 专业id
   */
  delete(id: string): Observable<ProfessionDTO> {
    return this.http.delete<ProfessionDTO>(`${ProfessionService.URL}/${id}`);
  }

  /**
   * 查找所有专业，可按需求排序
   * @param param 排序参数
   * @param pattern 排序模式: asc/desc
   */
  findAllBySort(param?: string, pattern?: string): Observable<Array<ProfessionDTO>> {
    const params = {};
    Object.assign(params, param ? { param } : {}, pattern ? { pattern } : {});
    return this.http.get<Array<ProfessionDTO>>(`${ProfessionService.URL}/all/sort`, params);
  }

  /**
   * 根据专业code，检查专业code是否已存在
   * @param code 专业code
   */
  checkoutCodeIsExist(code: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, code ? { code } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${ProfessionService.URL}/checkCodeExist`, params);
  }

  /**
   * 统计专业数量
   */
  count(): Observable<number> {
    return this.http.get<number>(`${ProfessionService.URL}/count`);
  }
}
